using System.Linq;

namespace ApiContract.Domain
{
    public static class GiftCardExtension
    {
        /// <summary>
        /// 查询该礼品卡余额
        /// </summary>
        /// <returns></returns>
        public static decimal GetGiftCardRemainingAmount(this GiftCard giftCard)
        {
            var result = giftCard.GiftCardUsageHistory.Aggregate(giftCard.Amount, (current, gcuh) => current - gcuh.UsedValue);

            if (result < decimal.Zero)
                result = decimal.Zero;

            return result;
        }

        /// <summary>
        /// 检查该礼品卡是否可用
        /// </summary>
        /// <returns></returns>
        public static bool IsGiftCardValid(this GiftCard giftCard)
        {
            if (!giftCard.IsGiftCardActivated)
                return false;

            var remainingAmount = GetGiftCardRemainingAmount(giftCard);
            return remainingAmount > decimal.Zero;
        }
    }
}